package cn.edu.zjicm.wordsnet_d.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.view.BookGridView;

public class WordBookForGridAdapter extends BaseAdapter {

	List<Integer> list;
	Context mContext;
	WordFactory wf;
	boolean addBtnFlag = false;
	private BookGridView bookGridView;

	public WordBookForGridAdapter(List<Integer> list, Context mContext, boolean addBtnFlag, BookGridView bookGridView) {
		this.list = list;
		this.mContext = mContext;
		wf = WordFactory.getInstance(mContext);
		this.addBtnFlag = addBtnFlag;
		this.bookGridView = bookGridView;
	}

	private int clickTemp = -1;

	public void setSelection(int position) {
		clickTemp = position;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

//		if (bookGridView.isOnMeasure) {
//			convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_book_item, false);
//			return convertView;
//		}

		MyHolder myHolder;
		convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_book_item, parent, false);
		myHolder = new MyHolder();
		myHolder.bookName = (TextView) convertView.findViewById(R.id.bookName);
		myHolder.letterNum = (TextView) convertView.findViewById(R.id.letterNum);
		myHolder.addBookBtn = (ImageView) convertView.findViewById(R.id.addBookBtn);

		if (clickTemp == position) {
			convertView.setBackgroundResource(R.drawable.book_seleced);
		} else {
			convertView.setBackgroundResource(R.drawable.book_default);
		}

//		int paddingTop = Utils.changeDipToPx(mContext, 15);
//		int paddingLeft = Utils.changeDipToPx(mContext, 5);
//		int paddingBottom = Utils.changeDipToPx(mContext, 25);

		if (position == list.size() - 1 && addBtnFlag) {
			myHolder.bookName.setVisibility(View.GONE);
			myHolder.letterNum.setText("添加新词汇书");
			myHolder.letterNum.setTextColor(mContext.getResources().getColor(R.color.board_first_text_color));
			myHolder.addBookBtn.setVisibility(View.VISIBLE);
//			paddingBottom = Utils.changeDipToPx(mContext, 38);
//			convertView.setPadding(paddingLeft, paddingTop, 0, paddingBottom);
		} else {
			myHolder.addBookBtn.setVisibility(View.GONE);
			myHolder.bookName.setVisibility(View.VISIBLE);
			myHolder.bookName.setText(WordFactory.getInstance(mContext).getBookName(AppPreference.Book.BOOK_ID[list.get(position)]));
			String str = wf.getUnLearnedWordCount(list.get(position)) + "/" + wf.getWordCountInBook(list.get(position));
			myHolder.letterNum.setText(str);
//			convertView.setPadding(paddingLeft, paddingTop, 0, paddingBottom);
		}

		return convertView;
	}

	class MyHolder {
		TextView bookName, letterNum;
		ImageView addBookBtn, bookBg;
	}
}
